#include "IntroHeader.h"
using namespace std;

void HandleData(vector<int> &data, char text[]);

vector<int> ReadData() {
	vector<int> data;
	int fd = open("data", O_RDONLY);

	char text[8193];
	int n;
	while ((n = read(fd, text, 8192)) > 0) {
		text[n] = '\0';
		HandleData(data, text);
	}
	if (data.back() == -1)
		data.pop_back();

	close(fd);

	return data;
}

int IntergerCount(int n);

void HandleData(vector<int> &data, char text[]) {
	int last, curpos = 0;
	bool flag = false;
	if (!data.empty() && data.back() == -1) {
		data.pop_back();
		last = data.back();
		flag = true;
	}
	if (text[0] == ' ') {
		curpos = 1;
		flag = false;
	}
	string temp;
	for (; curpos < strlen(text); ++curpos) {
		if (text[curpos] != ' ')
			temp.push_back(text[curpos]);
		else {
			if (flag) {
				int tempdata = atoi(temp.c_str());
				int count = IntergerCount(tempdata);
				data.pop_back();
				last *= pow(10, count);
				last += tempdata;
				data.push_back(last);
				flag = false;
			} else {
				int tempdata = atoi(temp.c_str());
				data.push_back(tempdata);
			}
			temp = "";
		}
	}
	if (!temp.empty()) {
		int tempdata = atoi(temp.c_str());
		data.push_back(tempdata);
		data.push_back(-1);
	}
}

int IntergerCount(int n) {
	int count = 1;
	while(n / 10) {
		++count;
	}

	return count;
}
